package cn.easytogo.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil
{

    public StringUtil()
    {
    }

    public static boolean stringIsNull(String s)
    {
        return s == null || "".equals(s.trim());
    }

    public static boolean isNull(String s)
    {
        return stringIsNull(s);
    }

    public static String iso2Gbk(String s) throws UnsupportedEncodingException
    {
        String s1 = new String(s.getBytes("ISO-8859-1"), "GBK");
        return s1;
    }

    public static String iso2Utf(String s) throws UnsupportedEncodingException
    {
        String s1 = new String(s.getBytes("ISO-8859-1"), "UTF-8");
        return s1;
    }

    public static String utf2Gbk(String s) throws UnsupportedEncodingException
    {
        String s1 = new String(s.getBytes("UTF-8"), "GBK");
        return s1;
    }

    public static String utf2Iso(String s) throws UnsupportedEncodingException
    {
        String s1 = new String(s.getBytes("UTF-8"), "ISO-8859-1");
        return s1;
    }

    public static String null2blank(String s)
    {
        return roundTrim(s);
    }

    public static String null2blank(Object obj)
    {
        return null2blank(String.valueOf(obj));
    }

    public static String null2Nbsp(String s)
    {
        if(s == null || s.length() == 0 || "null".equals(s.trim()))
            return "&nbsp;";
        else
            return s;
    }

    public static String null2Nbsp(Object obj)
    {
        return null2Nbsp(String.valueOf(obj));
    }

    public static String arrayToString(String as[], String s)
    {
        if(as == null)
            return "";
        if(as.length == 0)
            return "";
        int i = as.length - 1;
        if(s == null)
            s = "";
        StringBuffer stringbuffer = new StringBuffer();
        for(int j = 0; j < i; j++)
        {
            if(as[j] == null || as[j].trim().equals(""))
                continue;
            stringbuffer.append(as[j]);
            if(j != i - 1)
            {
                stringbuffer.append(s);
                continue;
            }
            if(as[i] != null && !as[i].trim().equals(""))
                stringbuffer.append(s);
        }

        if(as[i] != null && !as[i].trim().equals(""))
            stringbuffer.append(as[i]);
        return stringbuffer.toString();
    }

    public static String[] split(String s, String s1)
    {
        String as[] = new String[0];
        if(s == null)
            return as;
        if(s1 == null)
            s1 = "";
        StringTokenizer stringtokenizer = new StringTokenizer(s, s1);
        int i = stringtokenizer.countTokens();
        as = new String[i];
        for(int j = 0; j < i; j++)
            as[j] = stringtokenizer.nextToken();

        return as;
    }

    public static int[] findStringPos(String s, String s1)
    {
        int ai[] = new int[0];
        if(s == null || s.equals(""))
            return ai;
        if(s1 == null || s1.equals(""))
            return ai;
        ArrayList arraylist = new ArrayList();
        boolean flag = false;
        int i;
        for(int j = 0; (i = s.indexOf(s1, j)) != -1; j = i + s1.length())
            arraylist.add(new Integer(i));

        if(arraylist.size() > 0)
        {
            ai = new int[arraylist.size()];
            for(int k = 0; k < arraylist.size(); k++)
                ai[k] = ((Integer)arraylist.get(k)).intValue();

        }
        return ai;
    }

    public static String lpad(String s, int i, int j)
    {
        if(s == null || s.trim().equals(""))
            return s;
        if(i < 0 || j < 0)
            return "";
        if(i > s.length() - 1)
            return "";
        int k = i;
        int l = i + j;
        if(l > s.length())
            l = s.length();
        if(l < k)
        {
            int i1 = k;
            k = l;
            l = i1;
        }
        return s.substring(k, l);
    }

    public static String left(String s, int i, int j)
    {
        return lpad(s, i, j);
    }

    public static String left(String s, int i)
    {
        return lpad(s, 0, i);
    }

    public static String rpad(String s, int i)
    {
        if(s == null)
            return null;
        if(i < 0)
            return "";
        if(s.length() <= i)
            return s;
        else
            return s.substring(s.length() - i, s.length());
    }

    public static String right(String s, int i)
    {
        return rpad(s, i);
    }

    public static boolean matcher(String s, String s1)
    {
        return matcher(s, s1, false);
    }

    public static boolean matcherIgnoreCase(String s, String s1)
    {
        return matcher(s, s1, true);
    }

    private static boolean matcher(String s, String s1, boolean flag)
    {
        if(s == null)
            return false;
        if(s1 == null)
            return false;
        if(flag)
        {
            s = s.toLowerCase();
            s1 = s1.toLowerCase();
        }
        Pattern pattern = Pattern.compile(s1);
        Matcher matcher1 = pattern.matcher(s);
        return matcher1.find();
    }

    public static boolean contains(String as[], String s)
    {
        return contains(as, s, true);
    }

    public static boolean containsIgnoreCase(String as[], String s)
    {
        return contains(as, s, false);
    }

    private static boolean contains(String as[], String s, boolean flag)
    {
        for(int i = 0; i < as.length; i++)
        {
            String s1 = as[i];
            if(flag)
            {
                if(s1.equals(s))
                    return true;
                continue;
            }
            if(s1.equalsIgnoreCase(s))
                return true;
        }

        return false;
    }

    public static String removeFirst(String s, String s1)
    {
        if(s == null)
            return s;
        if(s1 == null)
            return s;
        Pattern pattern = Pattern.compile(s1);
        Matcher matcher1 = pattern.matcher(s);
        if(matcher1.find())
        {
            String s2 = matcher1.group();
            return s.replaceFirst(s2, "");
        } else
        {
            return s;
        }
    }

    public static String removeAll(String s, String s1)
    {
        if(s == null)
            return s;
        if(s1 == null)
            return s;
        Pattern pattern = Pattern.compile(s1);
        Matcher matcher1 = pattern.matcher(s);
        if(matcher1.find())
            s = matcher1.replaceAll("");
        return s;
    }

    public static String insert(String s, String s1, int i)
    {
        if(s == null)
            return s;
        if(s1 == null)
            return s;
        if(i < 0)
            return s;
        StringBuffer stringbuffer = new StringBuffer(s.length());
        stringbuffer.append(s);
        if(i > s.length() - 1)
            stringbuffer.append(s1);
        else
            stringbuffer.insert(i, s1);
        return stringbuffer.toString();
    }

    public static String roundTrim(String s)
    {
        if(s != null && !"null".equals(s))
            s = s.replaceAll("(^[ |\u3000]*|[ |\u3000]*$)", "");
        else
            s = "";
        return s;
    }

    public static final String toHexString(byte abyte0[])
    {
        if(abyte0 == null)
            return null;
        StringBuffer stringbuffer = new StringBuffer();
        for(int i = 0; i < abyte0.length; i++)
            stringbuffer.append(toHexString(abyte0[i]));

        return stringbuffer.toString();
    }

    private static final String toHexString(byte byte0)
    {
        int i = byte0;
        if(i < 0)
            i += 256;
        int j = i / 16;
        int k = i % 16;
        return hexDigits[j] + hexDigits[k];
    }

    public static String MD5Encode(String s)
    {
        String s1 = null;
        if(s == null)
            return s;
        try
        {
            s1 = new String(s);
            MessageDigest messagedigest = MessageDigest.getInstance("MD5");
            s1 = toHexString(messagedigest.digest(s1.getBytes()));
        }
        catch(NoSuchAlgorithmException nosuchalgorithmexception)
        {
        	nosuchalgorithmexception.printStackTrace();
        }
        return s1;
    }

    public static final String banjiao2quanjiao(String s)
    {
        String s1 = "";
        String s2 = "";
        byte abyte0[] = null;
        for(int i = 0; i < s.length(); i++)
        {
            try
            {
                s2 = s.substring(i, i + 1);
                abyte0 = s2.getBytes("unicode");
            }
            catch(UnsupportedEncodingException unsupportedencodingexception)
            {
            	unsupportedencodingexception.printStackTrace();
            }
            if(abyte0[3] != -1)
            {
                abyte0[2] = (byte)(abyte0[2] - 32);
                abyte0[3] = -1;
                try
                {
                    s1 = s1 + new String(abyte0, "unicode");
                }
                catch(UnsupportedEncodingException unsupportedencodingexception1)
                {
                	unsupportedencodingexception1.printStackTrace();
                }
            } else
            {
                s1 = s1 + s2;
            }
        }

        return s1;
    }

    public static final String quanjiao2banjiao(String s)
    {
        String s1 = "";
        String s2 = "";
        byte abyte0[] = null;
        for(int i = 0; i < s.length(); i++)
        {
            try
            {
                s2 = s.substring(i, i + 1);
                abyte0 = s2.getBytes("unicode");
            }
            catch(UnsupportedEncodingException unsupportedencodingexception)
            {
            	unsupportedencodingexception.printStackTrace();
            }
            if(abyte0[3] == -1)
            {
                abyte0[2] = (byte)(abyte0[2] + 32);
                abyte0[3] = 0;
                try
                {
                    s1 = s1 + new String(abyte0, "unicode");
                }
                catch(UnsupportedEncodingException unsupportedencodingexception1)
                {
                	unsupportedencodingexception1.printStackTrace();
                }
            } else
            {
                s1 = s1 + s2;
            }
        }

        return s1;
    }

    public static int toInt(String s, int i)
    {
        if(s == null || s.length() == 0)
            return i;
        int j = 0;
        try
        {
            j = Integer.valueOf(s).intValue();
        }
        catch(Exception exception)
        {
            j = i;
            exception.printStackTrace();
        }
        return j;
    }

    public static String toHtmlchars(String s)
    {
        if(s == null || 0 == s.length())
            return "";
        StringBuffer stringbuffer = new StringBuffer();
        for(int i = 0; i < s.length(); i++)
        {
            char c = s.charAt(i);
            switch(c)
            {
            case 34: // '"'
                stringbuffer.append("&quot;");
                break;

            case 38: // '&'
                stringbuffer.append("&amp;");
                break;

            case 60: // '<'
                stringbuffer.append("&lt;");
                break;

            case 62: // '>'
                stringbuffer.append("&gt;");
                break;

            case 10: // '\n'
                stringbuffer.append("<br>");
                break;

            case 9: // '\t'
                stringbuffer.append("&nbsp;&nbsp;&nbsp;&nbsp;");
                break;

            case 32: // ' '
                stringbuffer.append("&nbsp;");
                break;

            case 180: 
                stringbuffer.append("&acute;");
                break;

            case 166: 
                stringbuffer.append("&brvbar;");
                break;

            case 184: 
                stringbuffer.append("&cedil;");
                break;

            case 162: 
                stringbuffer.append("&cent;");
                break;

            case 710: 
                stringbuffer.append("&circ;");
                break;

            case 169: 
                stringbuffer.append("&copy;");
                break;

            case 164: 
                stringbuffer.append("&curren;");
                break;

            case 176: 
                stringbuffer.append("&deg;");
                break;

            case 247: 
                stringbuffer.append("&divide;");
                break;

            case 402: 
                stringbuffer.append("&fnof;");
                break;

            case 189: 
                stringbuffer.append("&frac12;");
                break;

            case 188: 
                stringbuffer.append("&frac14;");
                break;

            case 190: 
                stringbuffer.append("&frac34;");
                break;

            case 175: 
                stringbuffer.append("&macr;");
                break;

            case 181: 
                stringbuffer.append("&micro;");
                break;

            case 183: 
                stringbuffer.append("&middot;");
                break;

            case 338: 
                stringbuffer.append("&OElig;");
                break;

            case 339: 
                stringbuffer.append("&oelig;");
                break;

            case 170: 
                stringbuffer.append("&ordf;");
                break;

            case 186: 
                stringbuffer.append("&ordm;");
                break;

            case 248: 
                stringbuffer.append("&oslash;");
                break;

            case 182: 
                stringbuffer.append("&para;");
                break;

            case 177: 
                stringbuffer.append("&plusmn;");
                break;

            case 163: 
                stringbuffer.append("&pound;");
                break;

            case 187: 
                stringbuffer.append("&raquo;");
                break;

            case 174: 
                stringbuffer.append("&reg;");
                break;

            case 167: 
                stringbuffer.append("&sect;");
                break;

            case 173: 
                stringbuffer.append("&shy;");
                break;

            case 185: 
                stringbuffer.append("&sup1;");
                break;

            case 178: 
                stringbuffer.append("&sup2;");
                break;

            case 179: 
                stringbuffer.append("&sup3;");
                break;

            case 732: 
                stringbuffer.append("&tilde;");
                break;

            case 215: 
                stringbuffer.append("&times;");
                break;

            case 218: 
                stringbuffer.append("&Uacute;");
                break;

            case 168: 
                stringbuffer.append("&uml;");
                break;

            case 165: 
                stringbuffer.append("&yen;");
                break;

            case 8204: 
                stringbuffer.append("&zwnj;");
                break;

            case 8211: 
                stringbuffer.append("&ndash;");
                break;

            case 8212: 
                stringbuffer.append("&mdash;");
                break;

            case 8216: 
                stringbuffer.append("&lsquo;");
                break;

            case 8217: 
                stringbuffer.append("&rsquo;");
                break;

            case 8218: 
                stringbuffer.append("&sbquo;");
                break;

            case 8220: 
                stringbuffer.append("&ldquo;");
                break;

            case 8221: 
                stringbuffer.append("&rdquo;");
                break;

            case 8222: 
                stringbuffer.append("&bdquo;");
                break;

            case 8249: 
                stringbuffer.append("&lsaquo;");
                break;

            case 8250: 
                stringbuffer.append("&rsaquo;");
                break;

            case 8240: 
                stringbuffer.append("&permil;");
                break;

            case 8226: 
                stringbuffer.append("&bull;");
                break;

            case 8230: 
                stringbuffer.append("&hellip;");
                break;

            case 8243: 
                stringbuffer.append("&Prime;");
                break;

            case 8242: 
                stringbuffer.append("&prime;");
                break;

            case 8254: 
                stringbuffer.append("&oline;");
                break;

            case 8260: 
                stringbuffer.append("&frasl;");
                break;

            case 8482: 
                stringbuffer.append("&trade;");
                break;

            case 8592: 
                stringbuffer.append("&larr;");
                break;

            case 8593: 
                stringbuffer.append("&uarr;");
                break;

            case 8594: 
                stringbuffer.append("&rarr;");
                break;

            case 8595: 
                stringbuffer.append("&darr;");
                break;

            case 8706: 
                stringbuffer.append("&part;");
                break;

            case 8712: 
                stringbuffer.append("&isin;");
                break;

            case 8719: 
                stringbuffer.append("&prod;");
                break;

            case 8721: 
                stringbuffer.append("&sum;");
                break;

            case 8722: 
                stringbuffer.append("&minus;");
                break;

            case 8730: 
                stringbuffer.append("&radic;");
                break;

            case 8733: 
                stringbuffer.append("&prop;");
                break;

            case 8734: 
                stringbuffer.append("&infin;");
                break;

            case 8736: 
                stringbuffer.append("&ang;");
                break;

            case 8743: 
                stringbuffer.append("&and;");
                break;

            case 8744: 
                stringbuffer.append("&or;");
                break;

            case 8745: 
                stringbuffer.append("&cap;");
                break;

            case 8746: 
                stringbuffer.append("&cup;");
                break;

            case 8747: 
                stringbuffer.append("&int;");
                break;

            case 8756: 
                stringbuffer.append("&there4;");
                break;

            case 8776: 
                stringbuffer.append("&asymp;");
                break;

            case 8800: 
                stringbuffer.append("&ne;");
                break;

            case 8801: 
                stringbuffer.append("&equiv;");
                break;

            case 8804: 
                stringbuffer.append("&le;");
                break;

            case 8805: 
                stringbuffer.append("&ge;");
                break;

            case 8853: 
                stringbuffer.append("&oplus;");
                break;

            case 8869: 
                stringbuffer.append("&perp;");
                break;

            case 9674: 
                stringbuffer.append("&loz;");
                break;

            case 913: 
                stringbuffer.append("&Alpha;");
                break;

            case 945: 
                stringbuffer.append("&alpha;");
                break;

            case 914: 
                stringbuffer.append("&Beta;");
                break;

            case 946: 
                stringbuffer.append("&beta;");
                break;

            case 915: 
                stringbuffer.append("&Gamma;");
                break;

            case 947: 
                stringbuffer.append("&gamma;");
                break;

            case 916: 
                stringbuffer.append("&Delta;");
                break;

            case 948: 
                stringbuffer.append("&delta;");
                break;

            case 917: 
                stringbuffer.append("&Epsilon;");
                break;

            case 949: 
                stringbuffer.append("&epsilon;");
                break;

            case 918: 
                stringbuffer.append("&Zeta;");
                break;

            case 950: 
                stringbuffer.append("&zeta;");
                break;

            case 919: 
                stringbuffer.append("&Eta;");
                break;

            case 951: 
                stringbuffer.append("&eta;");
                break;

            case 920: 
                stringbuffer.append("&Theta;");
                break;

            case 952: 
                stringbuffer.append("&theta;");
                break;

            case 921: 
                stringbuffer.append("&Iota;");
                break;

            case 953: 
                stringbuffer.append("&iota;");
                break;

            case 922: 
                stringbuffer.append("&Kappa;");
                break;

            case 954: 
                stringbuffer.append("&kappa;");
                break;

            case 923: 
                stringbuffer.append("&Lambda;");
                break;

            case 955: 
                stringbuffer.append("&lambda;");
                break;

            case 924: 
                stringbuffer.append("&Mu;");
                break;

            case 956: 
                stringbuffer.append("&mu;");
                break;

            case 925: 
                stringbuffer.append("&Nu;");
                break;

            case 957: 
                stringbuffer.append("&nu;");
                break;

            case 926: 
                stringbuffer.append("&Xi;");
                break;

            case 958: 
                stringbuffer.append("&xi;");
                break;

            case 927: 
                stringbuffer.append("&Omicron;");
                break;

            case 959: 
                stringbuffer.append("&omicron;");
                break;

            case 928: 
                stringbuffer.append("&Pi;");
                break;

            case 960: 
                stringbuffer.append("&pi;");
                break;

            case 929: 
                stringbuffer.append("&Rho;");
                break;

            case 961: 
                stringbuffer.append("&rho;");
                break;

            case 931: 
                stringbuffer.append("&Sigma;");
                break;

            case 963: 
                stringbuffer.append("&sigma;");
                break;

            case 962: 
                stringbuffer.append("&sigmaf;");
                break;

            case 932: 
                stringbuffer.append("&Tau;");
                break;

            case 964: 
                stringbuffer.append("&tau;");
                break;

            case 933: 
                stringbuffer.append("&Upsilon;");
                break;

            case 965: 
                stringbuffer.append("&upsilon;");
                break;

            case 934: 
                stringbuffer.append("&Phi;");
                break;

            case 966: 
                stringbuffer.append("&phi;");
                break;

            case 935: 
                stringbuffer.append("&Chi;");
                break;

            case 967: 
                stringbuffer.append("&chi;");
                break;

            case 936: 
                stringbuffer.append("&Psi;");
                break;

            case 968: 
                stringbuffer.append("&psi;");
                break;

            case 937: 
                stringbuffer.append("&Omega;");
                break;

            case 969: 
                stringbuffer.append("&omega;");
                break;

            default:
                stringbuffer.append(c);
                break;
            }
        }

        return stringbuffer.toString();
    }

    private static String toTextChars(String s)
    {
        if(s == null || 0 == s.length())
        {
            return "";
        } else
        {
            s = s.replaceAll("<br>", "\n");
            s = s.replaceAll("&nbsp;", " ");
            s = s.replaceAll("&quot;", "\"");
            s = s.replaceAll("&nbsp;&nbsp;&nbsp;&nbsp;", "\t");
            s = s.replaceAll("&reg;", "\256");
            s = s.replaceAll("&lt;", "<");
            s = s.replaceAll("&gt;", ">");
            return s;
        }
    }

    public static String addQuoteToSqlValue(String s)
    {
        StringBuffer stringbuffer = new StringBuffer();
        if(s != null)
        {
            String as[] = s.split(",");
            int i = as.length;
            for(int j = 0; j < i; j++)
            {
                String s1 = as[j];
                stringbuffer.append("'").append(s1).append("'");
                if(j < i - 1)
                    stringbuffer.append(",");
            }

        }
        return stringbuffer.toString();
    }

    public static void main(String args[])
        throws Exception
    {
    }

    private static final String hexDigits[] = {
        "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", 
        "a", "b", "c", "d", "e", "f"
    };

}